<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">试剂柜/冰箱</label>
        <el-select
          v-model="query.cabinetId"
          clearable
          style="width: 185px;"
          placeholder="试剂柜/冰箱"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in cabinets"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <!-- <label class="el-form-item-label">类型</label>
        <el-select
          v-model="query.type"
          clearable
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
        <el-option label="标准物质" :value="2"/>
        <el-option label="化学试剂" :value="3"/>
        </el-select> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" >
        <template slot="right">
          <el-button :disabled="crud.selections.length==0" class="sellingBtn" size="mini" type="danger" @click="onSellingWarehouse('moreSell')">批量销库</el-button>
        </template>
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="amount" label="数量" :formatter="formatterUnit"/>
        <el-table-column prop="cabinetName" label="试剂柜/冰箱" />
        <el-table-column prop="expirationDate" label="有效期"/>

        <el-table-column
          v-if="checkPer(['admin', 'sensor:edit', 'sensor:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <!-- <udOperation :data="scope.row" :permission="permission" /> -->
            <el-button size="mini" type="danger" @click="onSellingWarehouse(scope.row)">销库</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import { delBurningAPI } from '@/api/hazardousChemicals'
import crudSensor from '@/api/sensor'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getCabinet,getAllCabinetsAPI } from '@/api/cabinet'

const defaultForm = {
  id: null,
  name: null,
  type: null,
  labId: null,
  isForbidden: 0,
  thresholdValue: null,
  createdAt: null,
  updatedAt: null,
  isDeleted: null,
  data: null,
  slaveStationId: null
}
export default {
  name: 'SellingWarehouse',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '销库',
      url: 'api/hazardousChemicals/expireList',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudSensor },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: true,
        reset: true,
      },
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'sensor:add'],
        edit: ['admin', 'sensor:edit'],
        del: ['admin', 'sensor:del']
      },
      queryTypeOptions: [
        { key: 'name', display_name: '名称' },
        { key: 'type', display_name: '传感器类型' },
        { key: 'labId', display_name: '实验室' },
      ],
      cabinets: [],
      storageOptions:[
        {
          value:'器材室',
          label: 1
        },
        {
          value:'备品室',
          label: 2
        },
        {
          value:'外业仓库',
          label: 3
        },
      ],
    }
  },
  created() {
    this.getCabinets()
  },
  methods: {
    async onSellingWarehouse(row){
      let ids =[]
      if(row == 'moreSell'){
        ids=this.crud.selections.map(item=>item.id)
      }else {
         ids =[row.id]
      }
       await delBurningAPI({ids})
       this.$notify.success('销库成功')
       this.crud.pageChangeHandler(0)
    },
    async getCabinets(query = '') {
      this.searchLoading = true
      const res = await getAllCabinetsAPI()
      this.cabinets = res
    },
    formatterUnit(row){
      let unit
      if(row.type == 2) {
        if(row.unit == 1){
          unit = '支'
        }else if(row.unit == 2){
          unit = '瓶'
        }
      }else if(row.type == 3) {
        unit = '瓶'
      }else if(row.type == 1) {
        if(row.unit == 1){
          unit = '个'
        }else if(row.unit == 2){
          unit = '台'
        }else {
          unit= '套'
        }
      }
      return row.amount.toString() + unit
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped lang="scss">
.sellingBtn {
 margin: 0 3px 10px 0;
 display: inline-block;
 vertical-align: middle;
}
</style>
